# -*- coding: utf-8 -*-
# @Time : 2024/3/27 11:46 AM 
# @Author : Wayne
# @Email : 1
# @File : t_group_sum.py
# @Project : young_python_demo
# @DESCRIPTION :分组与汇总

data = [
    {
        "companyName": "深圳市蓝禾技术有限公司",
        "completeTime": "",
        "consignTime": "2024-03-31",
        "customerCode": "C2024022310812",
        "data_create_time": "Mon, 25 Mar 2024 00:17:54 GMT",
        "data_update_time": "Mon, 25 Mar 2024 04:22:26 GMT",
        "goodsName": "图拉斯-智贴系列(五代)手机保护膜-蓝光-苹果-iPhone 15 Pro Max-带一体式助贴器",
        "goodsNo": "TSGP121A6IPH15PMBK1",
        "goodsPlatDiscountFee": "0.000000",
        "goodsTags": "",
        "id": 609,
        "isDelete": "0",
        "isFit": "0",
        "localPayment": "66.300000",
        "mainPostid": "",
        "mergeStatus": "1",
        "msg": "G201.02XSCKD24030010",
        "onlineTradeNo": "2064405543449920777",
        "payTime": "Fri, 23 Feb 2024 12:58:54 GMT",
        "platSkuId": "5358014490310",
        "sellCount": "1.000000",
        "sellTotal": "66.300000",
        "sellerMemo": "",
        "shareFavourableAfterFee": "66.300000",
        "shareOrderDiscountFee": "0.000000",
        "shareOrderPlatDiscountFee": "0.000000",
        "shopCode": "01",
        "shopName": "天猫图拉斯旗舰店",
        "shopTypeCode": "TMALL",
        "specId": "1890700847140897024",
        "subTradeId": "1891332020544536833",
        "sumOrderNumber": "20240331X1711500945",
        "taxFee": "0.000000",
        "toK3": "1",
        "toK3_allot": "0",
        "tradeFrom": "1",
        "tradeId": "1891332020146569600",
        "tradeNo": "JY2024022314452",
        "tradeStatus": "9090",
        "tradeType": "1",
        "unit": "Pcs",
        "warehouseCode": "GNC01",
        "warehouseName": "提姆本地发货仓"
    },
    {
        "companyName": "深圳市蓝禾技术有限公司",
        "completeTime": "",
        "consignTime": "2024-03-31",
        "customerCode": "C2024021527583",
        "data_create_time": "Mon, 25 Mar 2024 00:17:54 GMT",
        "data_update_time": "Mon, 25 Mar 2024 04:22:26 GMT",
        "goodsName": "图拉斯-智贴系列(五代)手机保护膜-蓝光-苹果-iPhone 15 Pro Max-带一体式助贴器",
        "goodsNo": "TSGP121A6IPH15PMBK1",
        "goodsPlatDiscountFee": "0.000000",
        "goodsTags": "",
        "id": 630,
        "isDelete": "0",
        "isFit": "0",
        "localPayment": "161.900000",
        "mainPostid": "",
        "mergeStatus": "1",
        "msg": "G201.02XSCKD24030010",
        "onlineTradeNo": "3778484943979117207,3779781228845117207,3788601518484117207",
        "payTime": "Thu, 15 Feb 2024 23:36:10 GMT",
        "platSkuId": "5358014490310",
        "sellCount": "1.000000",
        "sellTotal": "66.300000",
        "sellerMemo": "",
        "shareFavourableAfterFee": "62.440000",
        "shareOrderDiscountFee": "0.000000",
        "shareOrderPlatDiscountFee": "0.000000",
        "shopCode": "01",
        "shopName": "天猫图拉斯旗舰店",
        "shopTypeCode": "TMALL",
        "specId": "1890700847140897024",
        "subTradeId": "1891337323331782400",
        "sumOrderNumber": "20240331X1711500945",
        "taxFee": "0.000000",
        "toK3": "1",
        "toK3_allot": "0",
        "tradeFrom": "1",
        "tradeId": "1891337323273062145",
        "tradeNo": "JY2024022314847",
        "tradeStatus": "9090",
        "tradeType": "1",
        "unit": "Pcs",
        "warehouseCode": "GNC01",
        "warehouseName": "提姆本地仓"
    },
    {
        "companyName": "深圳市蓝禾技术有限公司",
        "completeTime": "",
        "consignTime": "2024-03-31",
        "customerCode": "C2024022311409",
        "data_create_time": "Mon, 25 Mar 2024 00:17:54 GMT",
        "data_update_time": "Mon, 25 Mar 2024 04:22:26 GMT",
        "goodsName": "图拉斯-智贴系列(五代)手机保护膜-蓝光-苹果-iPhone 15 Pro Max-带一体式助贴器",
        "goodsNo": "TSGP121A6IPH15PMBK1",
        "goodsPlatDiscountFee": "0.000000",
        "goodsTags": "",
        "id": 638,
        "isDelete": "0",
        "isFit": "0",
        "localPayment": "66.300000",
        "mainPostid": "",
        "mergeStatus": "1",
        "msg": "G201.02XSCKD24030010",
        "onlineTradeNo": "3788677191799681335",
        "payTime": "Fri, 23 Feb 2024 13:20:29 GMT",
        "platSkuId": "5358014490310",
        "sellCount": "1.000000",
        "sellTotal": "66.300000",
        "sellerMemo": "",
        "shareFavourableAfterFee": "66.300000",
        "shareOrderDiscountFee": "0.000000",
        "shareOrderPlatDiscountFee": "0.000000",
        "shopCode": "01",
        "shopName": "天猫图拉斯旗舰店",
        "shopTypeCode": "TMALL",
        "specId": "1890700847140897024",
        "subTradeId": "1891343045079662848",
        "sumOrderNumber": "20240331X1711500945",
        "taxFee": "0.000000",
        "toK3": "1",
        "toK3_allot": "0",
        "tradeFrom": "1",
        "tradeId": "1891343043969777920",
        "tradeNo": "JY2024022315255",
        "tradeStatus": "9090",
        "tradeType": "1",
        "unit": "Pcs",
        "warehouseCode": "GNC01",
        "warehouseName": "提姆本地发货仓"
    },
    {
        "companyName": "深圳市蓝禾技术有限公司",
        "completeTime": "",
        "consignTime": "2024-03-31",
        "customerCode": "C2024022310067",
        "data_create_time": "Mon, 25 Mar 2024 00:17:54 GMT",
        "data_update_time": "Mon, 25 Mar 2024 04:22:26 GMT",
        "goodsName": "图拉斯-智贴系列(五代)手机保护膜-蓝光-苹果-iPhone 15 Pro Max-带一体式助贴器",
        "goodsNo": "TSGP121A6IPH15PMBK1",
        "goodsPlatDiscountFee": "0.000000",
        "goodsTags": "",
        "id": 539,
        "isDelete": "0",
        "isFit": "0",
        "localPayment": "113.900000",
        "mainPostid": "",
        "mergeStatus": "1",
        "msg": "G201.02XSCKD24030010",
        "onlineTradeNo": "2064116100769073167",
        "payTime": "Fri, 23 Feb 2024 12:31:36 GMT",
        "platSkuId": "5358014490310",
        "sellCount": "1.000000",
        "sellTotal": "66.300000",
        "sellerMemo": "",
        "shareFavourableAfterFee": "66.300000",
        "shareOrderDiscountFee": "0.000000",
        "shareOrderPlatDiscountFee": "0.000000",
        "shopCode": "01",
        "shopName": "天猫图拉斯旗舰店",
        "shopTypeCode": "TMALL",
        "specId": "1890700847140897024",
        "subTradeId": "1891318331264828545",
        "sumOrderNumber": "20240331X1711500945",
        "taxFee": "0.000000",
        "toK3": "1",
        "toK3_allot": "0",
        "tradeFrom": "1",
        "tradeId": "1891318330726842752",
        "tradeNo": "JY2024022313434",
        "tradeStatus": "9090",
        "tradeType": "1",
        "unit": "Pcs",
        "warehouseCode": "GNC01",
        "warehouseName": "提姆本地发货仓"
    }
]

"""
兼容了显示字段不存在原数据的
data_list 数据列表
group_fields 分组字段
sum_fields  汇总字段
watch_fields 显示字段
"""
def sum_by_group(data_list, group_fields, sum_fields, watch_fields):
    grouped_data = {}
    for info in data_list:
        key = tuple(info.get(field, '') for field in group_fields)
        if key not in grouped_data:
            grouped_data[key] = {
                field: float(info.get(field, 0)) if field in sum_fields else info.get(field, '')
                for field in set(group_fields + sum_fields + watch_fields)
            }
        else:
            for field in sum_fields:
                grouped_data[key][field] += float(info.get(field, 0))

    result_sum = []
    for key in grouped_data:
        temp_dict = {}
        for field in watch_fields:
            temp_dict[field] = grouped_data[key].get(field, '')
        # 确保汇总字段也在结果中
        temp_dict.update({field: str(grouped_data[key][field]) for field in sum_fields})
        result_sum.append(temp_dict)
    return result_sum


# 分组字段
group_fields = ["goodsNo", "sumOrderNumber", "shopCode", "shopName", "warehouseCode"]
# 显示字段
watch_fields = ["goodsNo", "sumOrderNumber", "shopCode", "shopName", "warehouseCode", "warehouseName", "companyName",
                "consignTime", "unit", "goodsTags", "tradeType", "aaa"]
# 汇总字段
sum_fields = ["sellCount", "sellTotal", "shareFavourableAfterFee"]

data_sum = sum_by_group(data, group_fields, sum_fields, watch_fields)
